package st.business.tepth.business.user;

import com.alibaba.fastjson.TypeReference;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.LocationBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * @author zhangyu
 * @date 2019/3/6
 */
public class UserModel implements UserContract.Model {
    @Override
    public Disposable getLocationInfo(String id, String token,
                                      final OnDataConvertListener<LocationBean> listener) {
        return RxRestClient.builder()
                .url(Url.GetWorkStationInfo)
                .params("id", id)
                .params("token", token)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToVisitorLocation(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .get();
    }

    @Override
    public Disposable offWork(String token, final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.OffWork)
                .params("token", token)
                .params("type", 1)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .get();
    }

    private LocationBean convertToVisitorLocation(String response) {
        return new ObjConverter<LocationBean>().convert(response
                , new TypeReference<LocationBean>() {
                });
    }
}
